/**
 * Copyright (c) 2004-2015 All Rights Reserved.
 */
package com.zbest.zpay.web.controller.gateway;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zbest.zpay.service.gw.MessageSenderService;
import com.zbest.zpay.service.gw.model.GwResult;
import com.zbest.zpay.web.controller.BaseController;
import com.zbest.zpay.web.controller.member.MerchantController;

/**
 * @author czhou3306@gmail.com
 * @version $Id: MerchantController.java, v0.1 2015年11月16日 下午7:57:18 czhou3306@gmail.com Exp $
 */
@Controller
public class WeixinController extends BaseController {

    @Resource
    private MessageSenderService messageSenderService;

    private static final Logger  logger = LoggerFactory.getLogger(MerchantController.class);

    @RequestMapping(value = "/weixin/gateway", method = RequestMethod.POST)
    public String createQrcodeTicket(HttpServletRequest request, HttpServletResponse response) {
        //FIXME 
        GwResult result = messageSenderService.syncReceive(null, null);
        return null;
    }

    /*

    private static final Logger  logger = LoggerFactory.getLogger(MerchantController.class);

    @Resource
    private MerchantRepository   merchantRepository;

    @Resource
    private AcctransQueryService acctransQueryService;

    *//**
      * 产生微信临时二维码的ticket
      *
      * @param request
      * @param createQrcodeTicketForm
      * @return
      */
    /*
    @RequestMapping(value = "/merchant/createQrcodeTicket", method = RequestMethod.POST)
    @ResponseBody
    public String createQrcodeTicket(HttpServletRequest request,
                                  @RequestBody CreateQrcodeTicketForm createQrcodeTicketForm) {
     logger.info("/merchant/createQrcodeTicket请求参数:" + createQrcodeTicketForm);
     Map<String, Object> data = new HashMap<String, Object>();
     data.put(CreateQrcodeTicketForm.class.getSimpleName(), createQrcodeTicketForm);
     ProcessResult processResult = processService.execute(
         ProcessConstant.PROCESS_CREATE_QRCODE_TICKET, data);
     if (processResult.isSuccess()) {
         return newSuccessResult(processResult.getResultObject());
     } else {
         return newErrorResult(processResult.getCode(), processResult.getCode().getMessage());
     }
    }

    *//**
      * 登录
      *
      * @param request
      * @param loginForm
      * @return
      */
    /*
    @RequestMapping(value = "/merchant/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(HttpServletRequest request, @RequestBody LoginForm loginForm) {

     Map<String, Object> data = new HashMap<String, Object>();
     data.put(LoginForm.class.getSimpleName(), loginForm);

     ProcessResult processResult = processService.execute(ProcessConstant.PROCESS_LOGIN, data);

     if (processResult.isSuccess()) {
         Merchant merchant = (Merchant) processResult.getResultObject().get(
             ContextField.KEY_MERCHANT);
         request.getSession().setAttribute(ContextField.KEY_MERCHANT, merchant);

         MerchantView merchantView = buildMerchantView(merchant);
         Map<String, Object> result = new HashMap<String, Object>();
         result.put(ViewField.KEY_MERCHANT, merchantView);

         return newSuccessResult(result);
     } else {
         return newErrorResult(processResult.getCode(), processResult.getCode().getMessage());
     }
    }

    *//**
      * 退出登录
      *
      * @param request
      * @param loginForm
      * @return
      */
    /*
    @RequestMapping(value = "/merchant/logout", method = RequestMethod.POST)
    @ResponseBody
    public String logout(HttpServletRequest request) {

     request.getSession().removeAttribute(ContextField.KEY_MERCHANT);
     return newSuccessResult();

    }

    *//**
      * @param request
      * @param merchant
      * @return
      */
    /*
    private MerchantView buildMerchantView(Merchant merchant) {
     MerchantView merchantView = new MerchantView();
     merchantView.setGmtCreate(CoreDateUtils.standardFormat(merchant.getGmtCreate()));
     merchantView.setIndustry(merchant.getIndustry());
     merchantView.setMerchantId(merchant.getMerchantId());
     merchantView.setMerchantName(merchant.getMerchantName());
     merchantView.setMobileNo(merchant.getMobileNo());
     merchantView.setStatus(merchant.getStatus());
     return merchantView;
    }

    @RequestMapping(value = "/merchant/check", method = RequestMethod.POST)
    @ResponseBody
    public String check(HttpServletRequest request, @RequestBody MerchatCheckForm merchatCheckForm) {
     logger.info("/merchant/check请求参数:" + merchatCheckForm);
     //参数校验
     if (StringUtils.isBlank(merchatCheckForm.getMobileNo())) {
         return newErrorResult(MarketBizCodeEnum.REQUEST_PARAM_INVALID);
     }
     Map<String, Object> data = new HashMap<String, Object>();
     data.put(MerchatCheckForm.class.getSimpleName(), merchatCheckForm);
     ProcessResult processResult = processService.execute(
         ProcessConstant.PROCESS_MERCHANT_CHECK, data);
     if (processResult.isSuccess()) {
         return newSuccessResult(processResult.getResultObject());
     } else {
         return newErrorResult(processResult.getCode(), processResult.getCode().getMessage());
     }
    }

    @RequestMapping(value = "/merchant/register", method = RequestMethod.POST)
    @ResponseBody
    public String register(HttpServletRequest request, @RequestBody RegisterForm registerForm) {
     logger.info("/merchant/register请求参数:" + registerForm);
     //参数校验
     if (StringUtils.isBlank(registerForm.getMobileNo())) {
         return newErrorResult(MarketBizCodeEnum.REQUEST_PARAM_INVALID);
     }
     Map<String, Object> data = new HashMap<String, Object>();
     data.put(RegisterForm.class.getSimpleName(), registerForm);
     ProcessResult processResult = processService
         .execute(ProcessConstant.PROCESS_REGISTER, data);
     if (processResult.isSuccess()) {
         Merchant merchant = (Merchant) processResult.getResultObject().get(
             ContextField.KEY_MERCHANT);
         request.getSession().setAttribute(ContextField.KEY_MERCHANT, merchant);

         MerchantView merchantView = buildMerchantView(merchant);
         Map<String, Object> result = new HashMap<String, Object>();
         result.put(ViewField.KEY_MERCHANT, merchantView);

         return newSuccessResult(result);
     } else {
         return newErrorResult(processResult.getCode(), processResult.getCode().getMessage());
     }
    }

    @RequestMapping(value = "/merchant/queryAuthorizerApp", method = RequestMethod.GET)
    @ResponseBody
    public String queryAuthorizerApp(HttpServletRequest request) {

     Merchant merchant = (Merchant) request.getSession().getAttribute(ContextField.KEY_MERCHANT);
     Map<String, Object> data = new HashMap<String, Object>();
     data.put(ContextField.KEY_MERCHANT_ID, merchant.getMerchantId());

     ProcessResult processResult = processService.execute(
         ProcessConstant.PROCESS_QUERY_AUTHORIZERAPP, data);

     if (processResult.isSuccess()) {

         return newSuccessResult(processResult.getResultObject());
     } else {
         return newErrorResult(processResult.getCode(), processResult.getCode().getMessage());
     }

    }

    @RequestMapping(value = "/merchant/queryAmount", method = RequestMethod.GET)
    @ResponseBody
    public String queryAmount(HttpServletRequest request) {

     Merchant merchant = (Merchant) request.getSession().getAttribute(ContextField.KEY_MERCHANT);
     Map<String, Object> data = new HashMap<String, Object>();
     data.put(ContextField.KEY_MERCHANT_ID, merchant.getMerchantId());

     ProcessResult processResult = processService.execute(ProcessConstant.PROCESS_QUERY_AMOUNT,
         data);

     if (processResult.isSuccess()) {

         return newSuccessResult(processResult.getResultObject());
     } else {
         return newErrorResult(processResult.getCode(), processResult.getCode().getMessage());
     }

    }

    @RequestMapping(value = "/merchant/queryInvites", method = RequestMethod.GET)
    @ResponseBody
    public String queryInvites(HttpServletRequest request,
                            @RequestParam(required = false) String pageNo,
                            @RequestParam(required = false) String pageSize) {

     Merchant merchant = (Merchant) request.getSession().getAttribute(ContextField.KEY_MERCHANT);

     List<Merchant> merchantList = merchantRepository.getByInvitor(merchant.getMobileNo(),
         StringUtils.isBlank(pageNo) ? 1 : Integer.parseInt(pageNo),
         StringUtils.isBlank(pageSize) ? ContextConstant.PAGE_SIZE : Integer.parseInt(pageSize));

     List<MerchantView> viewList = new ArrayList<MerchantView>();
     for (Merchant mer : merchantList) {
         viewList.add(buildMerchantView(mer));
     }

     int count = merchantRepository.getCountByInvitor(merchant.getMobileNo());

     Map<String, Object> result = new HashMap<String, Object>();
     result.put(ViewField.KEY_INVITE_LIST, viewList);
     result.put(ViewField.KEY_TOTAL_ITEMS, count);

     return newSuccessResult(result);
    }

    *//**
      * 查询收支明细
      * 
      * @param request
      * @param pageNo
      * @param pageSize
      * @return
      */
    /*
    @RequestMapping(value = "/incomeExpense/serials", method = RequestMethod.GET)
    @ResponseBody
    public String incomeExpense(HttpServletRequest request,
                             @RequestParam(required = false) String pageNo,
                             @RequestParam(required = false) String pageSize) {

     Merchant merchant = (Merchant) request.getSession().getAttribute(ContextField.KEY_MERCHANT);

     QryMainDetailsPageRequest qryMainDetailsPageRequest = new QryMainDetailsPageRequest();
     qryMainDetailsPageRequest.setUsrId(merchant.getMerchantId());
     qryMainDetailsPageRequest.setPageNo(StringUtils.isBlank(pageNo) ? 1 : Integer
         .parseInt(pageNo));
     qryMainDetailsPageRequest
         .setItemPerPage(StringUtils.isBlank(pageSize) ? ContextConstant.PAGE_SIZE : Integer
             .parseInt(pageSize));
    // qryMainDetailsPageRequest.setStartDat("20160719");
    // qryMainDetailsPageRequest.setEndDat("20160721");

     QryMainDetalsPageResponse response = acctransQueryService
         .queryMainDetails(qryMainDetailsPageRequest);

     Map<String, Object> result = new HashMap<String, Object>();
     result.put(ViewField.KEY_ACCTRANS_DETAIL_LIST, response.getMainAccountDetails());
     result.put(ViewField.KEY_TOTAL_ITEMS, response.getTotalItem());

     return newSuccessResult(result);
    }

    */
}
